package com.weibo.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import weibo4j.Status;
import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;

import com.weibo.db.DataBaseConnection;
import com.weibo.entity.JianceWeibo;

public class JianceWeiboTextService extends  DataBaseConnection{

	
	public void saveWeibo(){
		//获得全部的微博账号
		JianceWeiboService weiboservice = new JianceWeiboService();
		List<JianceWeibo> wbs = weiboservice.list();
		//根据微博ID存取微博内容
		for(JianceWeibo jw:wbs){
			int weiboid = jw.getWeibo_id();
			saveJianceWeiboTextById(weiboid);
		}
	}
	
	/**
	 * 根据微博的ID获得新浪微博数据
	 * @param id
	 * @return
	 */
	public void saveJianceWeiboTextById(int id){
		
		java.util.Date date=new java.util.Date();
		Timestamp saomiaoriqi=new Timestamp(date.getTime());
		
		User u = null;

		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
    	System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		Weibo weibo = new Weibo();
		weibo.setToken("656864a57af10603bed431ff5ce00e16","994609030214df396ff1c0ea31009ce8");
		
		List<Status> ss = null;
		try {
			ss = weibo.getUserTimeline(id+"");
		} catch (WeiboException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		for(Status s : ss){

			Timestamp ttt=new Timestamp(s.getCreatedAt().getTime());
			String sql = "insert into t_jiance_text(weibo_name,weibo_id,text_id,text,img_url,faburiqi,saomiaoriqi) " +
					"values('"+s.getUser().getScreenName()+"','"+id+"','"+s.getId()+"','"+s.getText()+"','"+s.getOriginal_pic()+"','"+ttt+"','"+saomiaoriqi+"')";
			System.out.println("sql:" + sql);
			
			try {
				super.save(sql);
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				super.closeAll();
			}
		}
	}
	
	public static void main(String[] args) {
		JianceWeiboTextService j = new JianceWeiboTextService();
		j.saveWeibo();
	}

}
